
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;

    return currentdate;
}


var curDate = new Date();
var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天

//显示前一天日期
function formatterDate(date) {
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
        + (date.getMonth() + 1);
    var hor = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    return date.getFullYear() + '-' + month + '-' + day + " " + hor + ":" + min + ":" + sec;

};




function getBeforeDate(n) {
    var n = n;
    var d = new Date();
    var year = d.getFullYear();
    var mon = d.getMonth() + 1;
    var day = d.getDate();
    if (day <= n) {
        if (mon > 1) {
            mon = mon - 1;
        }
        else {
            year = year - 1;
            mon = 12;
        }
    }
    d.setDate(d.getDate() - n);
    year = d.getFullYear();
    mon = d.getMonth() + 1;
    day = d.getDate();
    s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
    return s;
}


var TimeDone = null;
laydate.render({
    elem: '#dataTime'
    , type: 'datetime'
    ,
    done: function (value, date, endDate) {
        getTimeBegin(value);
    }
});

var doneTime = null;

function drawBar(mTitle, mName, mData, xLabel, mSelect, id) {
    var myChart = echarts.init(document.getElementById(id));
    var option = {
        title: {
            text: mTitle,
            x: 'center'
        },
        tooltip: {
            show: true
        },
        legend: {
            data: mName,
            padding: 5,
            padding: 10,
            itemGap: 20,
            selected: mSelect,
            x: 'right'
        },
        xAxis: [
            {
                type: 'category',
                data: xLabel
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: mData

    };
    myChart.setOption(option);
}

function getPiedata(res) {
    var level0 = 0;
    var level1 = 0;
    var level2 = 0;
    var level3 = 0;
    var level4 = 0;
    var level5 = 0;
    var dt = [];
    for (var i in res) {
        if (res[i].air_status == '优') {
            level0++;
        }
        if (res[i].air_status == '良') {
            level1++;
        }
        if (res[i].air_status == '轻度污染') {
            level2++;
        }
        if (res[i].air_status == '中度污染') {
            level3++;
        }
        if (res[i].air_status == '重度污染') {
            level4++;
        }
        if (res[i].air_status == '严重污染') {
            level5++;
        }
    }
    dt.push(level0);
    dt.push(level1);
    dt.push(level2);
    dt.push(level3);
    dt.push(level4);
    dt.push(level5);
    //var dt=[12,20,35,15,68,12];
    var data1 = [
        {value: dt[0], name: '优'},
        {value: dt[1], name: '良'},
        {value: dt[2], name: '轻度污染'},
        {value: dt[3], name: '中度污染'},
        {value: dt[4], name: '重度污染'},
        {value: dt[5], name: '严重污染'}
    ];
    getData5(dt);
    return data1;
}

function getData5(dt) {
    var mSelect = {'aqi': 1, 'pm2.5': 1, 'pm10': 1, 'so2': 1, 'no2': 1, 'co': 1, 'o3': 1}
    var cl = ['优', '良', '轻度污染', '中度污染', '重度污染', '严重污染'];
    var cldata = [{
        "name": "城市个数:",
        "type": "bar",
        "data": dt
    }];
    drawBar('空气质量级别城市个数', [''], cldata, cl, mSelect, 'block4');
}

/*
			data1=[
					   {value:_allData[1][i], name:'AQI'},
					   {value:_allData[2][i], name:'PM2.5'},
					   {value:_allData[3][i], name:'PM10'},
					   {value:_allData[4][i], name:'SO2'},
					   {value:_allData[5][i], name:'NO2'},
					   {value:_allData[6][i], name:'CO'},
					   {value:_allData[7][i], name:'O3'}
				];
*/
function drawpie(data, id) {
    var el = document.getElementById(id);
    var mychart1 = echarts.init(el);
    var option = {
        title: {
            text: '空气质量级别城市个数',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        series: [
            {
                name: '城市个数：',
                type: 'pie',
                radius: '50%',
                center: ['50%', '50%'],
                data: data,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)',
                    }
                }
            }
        ],
        color: ['rgb(0,228,0)', 'rgb(255,255,0)', 'rgb(255,126,0)', 'rgb(255,0,0)', 'rgb(153,0,76)', 'rgb(126,0,35)']
    };
    mychart1.setOption(option);
}

function addLaydata(id) {
    var curDate = new Date();
    var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天
    laydate.render({
        elem: id,
        type: 'datetime',
        position: 'static',
        value: formatterDate(preDate)
        ,isInitValue: true, //是否允许填充初始值，默认为 true

        done: function (value, date, endDate) {
            var doneTime = value;
            ajax(doneTime);
        }
    });
}

addLaydata('#data');
doneTime = getBeforeDate(1);
ajax(doneTime);

function ajax(doneTime) {
    var mData = [];
    var sliceSum = [];
    var slice_nums = [];
    var cn = [];


    /*var timenow = getNowFormatDate();*/


    var timenow = getBeforeDate(1)



    var doneTime = doneTime || timenow;

    doneTime = doneTime.trim().split(/\s+/)[0] + " 00:00:00";

    $.get('/hbtserver/aqi/city/day?day=' + doneTime + '', function (res) {
        var mName = ['aqi', 'pm2.5', 'pm10', 'so2', 'no2', 'co', 'o3'];
        var airType = ['aqi', 'pm25', 'pm10', 'so2', 'no2', 'co', 'o3'];
//获取mData
        for (var i in airType) {
            for (var m in res) {
                sliceSum.push(res[m][airType[i]]);
            }
        }
        for (var i = 0, len = sliceSum.length; i < len; i += res.length) {
            slice_nums.push(sliceSum.slice(i, i + res.length));
        }
        for (var i in airType) {
            mData.push({
                name: mName[i],
                type: "bar",
                data: slice_nums[i]
            })
        }
//获取cn城市名
        for (var i in res) {
            cn.push(res[i].name);
        }
//获取mSelect
        var mSelect = {'aqi': 1, 'pm2.5': 1, 'pm10': 1, 'so2': 1, 'no2': 1, 'co': 1, 'o3': 1}
        var cl = ['优', '良', '轻度污染', '中度污染', '重度污染', '严重污染'];
        showMap(res);
        drawBar('全省18个城市各指标值', mName, mData, cn, mSelect, 'chart1');
        //getPiedata(res);
        drawpie(getPiedata(res), 'block2');
        //drawBar('空气质量级别城市个数',[''],getData5(dt),cl,mSelect,'block4');
        drawBar('', mName, mData, cn, mSelect, 'block5');
    })
}

// var mName=['aqi','pm2.5','pm10','so2','no2','co','o3'];
// var mData=[{
//				"name":"pm10",
//				"type":"bar",
//				"data":[5, 200, 40, 10, 10, 20,5, 20, 40, 10, 10, 20,5, 20, 40, 10, 10, 20]
//			},
//			{
//					"name":"aqi",
//					"type":"bar",
//					"data":[5, 20, 400, 10, 10, 20,5, 20, 40, 10, 10, 20,5, 20, 40, 10, 10]
//			},
//			 {
//				"name":"pm2.5",
//				"type":"bar",
//				"data":[500, 20, 40, 10, 10, 20,5, 20, 40, 10, 10, 20,5, 20, 40, 10, 10, 20]
//			}];
// var mSelect={
// 'aqi':0,
// 'pm2.5':0,
// 'pm10':1,
// 'so2':0,
// }
// var cn=["郑州","开封","洛阳","平顶山","安阳","鹤壁","新乡","焦作","濮阳","许昌","漯河","三门峡","商丘","周口","驻马店","南阳","信阳"];
//	var cl=['优','良','轻度污染','中度污染','重度污染','严重污染'];
//	var cldata=[{
//				"name":"城市个数:",
//				"type":"bar",
//				"data":[5, 2, 6, 1, 0, 2]
//			}];
//drawBar('全省17个城市各指标值',mName,mData,cn,mSelect,'chart1');
//drawpie(getPiedata(res),'block2');
//drawBar('空气质量级别城市个数',[''],cldata,cl,mSelect,'block4');


//drawBar('',mName,mData,cn,mSelect,'block5');

function showMap(res) {
    var myData = [];
    for (var i in res) {
        myData.push({name: res[i].name, value: res[i].aqi});
    }
    $.get('hn.json', function (chinaJson) {
        echarts.registerMap('hn', chinaJson); // 注册地图
        var mapChart = echarts.init(document.getElementById('block3'));
        option = {
            title: {
                text: '全省环境指数分布',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: name
            },
            dataRange: {
                min: 0,
                max: 400,
                color: ['rgb(126,0,35)', 'rgb(153,0,76)', 'rgb(255,0,0)', 'rgb(255,126,0)', 'rgb(255,255,0)', 'rgb(0,228,0)'],
                text: ['优', '严重污染'],           // 文本，默认为数值文本
                calculable: true
            },
            series: [
                {
                    name: 'AQI',
                    type: 'map',
                    mapType: 'hn',
                    selectedMode: 'single',
                    itemStyle: {
                        normal: {label: {show: true}},
                        emphasis: {label: {show: true}}
                    },
                    data: myData
                }
            ]
        };
        mapChart.setOption(option, true);
    });
}

//	$(document).ready(function(){
//	$.get('http://127.0.0.1:15000/hbtserver/station?type=shi',function(rt,st){
//		var shiData=$.parseJSON(rt);
//	});
//
//	});